// pages/detail/detail.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nowYear: new Date().getFullYear(),
    id: '',
    workData: {
      title: 'loading',
      art_id: 0,
      aut_id: 0,
      aut_name: 'loading',
      aut_photo: 'https://api.uuz.bid/null.jpg',
      is_followed: false,
      content: '<p>加载中</p>',
      is_collected: false,
      pubdate: "2019-03-11 09:00:00"
    },
    loging: false,
    followLading: false,
    dailog: false,
    chatInput: '',
    maxlength: 50,
    art_id: null,
    showLoadChat: true,
    loadEnd: true,
    chatType: 'a',
    chatData: {
      end_id: '114514',
      last_id: '19191',
      results: [],
      total_count: 0
    },
    chatList: [],
    repeatDailog: false,
    repeatIndex: 0,
    repeatId: '',
    repeatList: [],
    target: null
  },
  showRepeat(e){
    console.log(e);
    this.setData({
      repeatDailog: true,
      chatType: 'c',
      repeatIndex: e.target.dataset.index,
      repeatId: e.target.dataset.id
    })
    this.getChat(e.target.dataset.id, this.data.chatList[e.target.dataset.index].reply_count)
  },
  closeRepeat(){
    this.setData({
      repeatDailog: false,
      chatType: 'a',
      repeatId: this.data.id
    })
    this.getNewChat(this.data.workData.art_id)
  },
  shwoDailog(e){
    console.log(e.target.dataset.id);
    if (this.data.loging) {
      this.setData({
        dailog: true,
        art_id: e.target.dataset.id,
        target: e.target.dataset.target
      })
    } else {
      wx.showToast({
        title: '请先登录',
        icon: 'error'
      })
    }
  },
  closeDailog(){
    this.setData({
      dailog: false,
      art_id: null,
      chatInput: '',
      repeatList: []
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options?.id);
    this.setData({
      id: options?.id
    })
    wx.getStorage({
      key: 'tokens',
      success: () => {
        this.setData({
          loging: true
        })
      },
      fail: ()=> {
        this.setData({
          loging: false
        })
      }
    })
    this.getWork()
  },
  getWork() {
    wx.showLoading({
      title: '载入中',
    })
    wx.request({
      url: app.globalData.baseURL +  '/articles/' + this.data.id,
      method: 'GET',
      success: (res) => {
        console.log(res);
        this.setData({
          workData: res.data.data
        })
        this.getChat(this.data.workData.art_id)
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },
  followStatus() {
    this.setData({
      followLading: !this.data.followLading
    })
  },
  followLUser(){
    this.setData({
      workData: {
        ...this.data.workData,
        is_followed: !this.data.workData.is_followed
      }
    })
  },
  follow(){
    if (!this.data.followLading) {
      this.followStatus()
      if (this.data.workData.is_followed) {
        wx.request({
          url: app.globalData.baseURL + '/user/followings/' + this.data.workData.aut_id,
          method: 'DELETE',
          header: {
            Authorization: app.globalData.Authorization
          },
          success: ()=>{
            this.followStatus()
            this.followLUser()
          }
        })
      } else {
        wx.request({
          url: app.globalData.baseURL + '/user/followings',
          method: 'POST',
          header: {
            Authorization: app.globalData.Authorization
          },
          data: {
            target: this.data.workData.aut_id
          },
          success: ()=>{
            this.followStatus()
            this.followLUser()
          }
        })
      }
    }
  },
  starUser(){
    this.setData({
      workData: {
        ...this.data.workData,
        is_collected: !this.data.workData.is_collected
      }
    })
  },
  star(){
    if (this.data.workData.is_collected) {
      wx.request({
        url: app.globalData.baseURL + '/article/collections/' + this.data.workData.art_id,
        method: 'DELETE',
        header: {
          Authorization: app.globalData.Authorization
        },
        success: ()=>{
          this.starUser()
        }
      })
    } else {
      wx.request({
        url: app.globalData.baseURL + '/article/collections',
        method: 'POST',
        header: {
          Authorization: app.globalData.Authorization
        },
        data: {
          target: this.data.workData.art_id
        },
        success: ()=>{
          this.starUser()
        }
      })
    }
  },
  pushChat(){
    wx.showLoading({
      title: '提交中',
    })
    wx.request({
      url: app.globalData.baseURL + '/comments',
      method: 'POST',
      header: {
        Authorization: app.globalData.Authorization
      },
      data: {
        art_id: this.data.art_id,
        content: this.data.chatInput,
        target: this.data.target
      },
      success: ()=>{
        this.closeDailog()
        if(this.data.repeatId.length <= 8){
          this.getNewChat(this.data.workData.art_id)
        } else {
          this.getNewChat(this.data.repeatId, this.data.chatList[this.data.repeatIndex].reply_count)
        }
        wx.showToast({
          title: '提交成功',
          icon: 'success'
        })
      },
      complete:()=>{
        wx.hideLoading()
      }
    })
  },
  getChat(id, size = 10){
    if (id.length <= 8){
      this.setData({
        showLoadChat: false
      })
    }
    wx.request({
      url: app.globalData.baseURL + '/comments',
      method: 'GET',
      data: {
        type: this.data.chatType,
        source: id,
        limit: size
      },
      success: (res) => {
        console.log(res);
        if (id.length <= 8) {
          if (this.data.chatList.length >= res.data.data.total_count) {
            this.setData({
              loadEnd: false,
              showLoadChat: true,
            })
          } else {
            this.setData({
              showLoadChat: true,
              chatData: res.data.data,
              chatList: [...this.data.chatList, ...res.data.data.results]
            })
          }
        } else {
          this.setData({
            repeatList: res.data.data.results
          })
        }
      }
    })
  },
  getNewChat(id, size = 10){
    if(this.data.repeatId.length <= 8){
      this.setData({
        showLoadChat: true,
        loadEnd: true,
        chatData: {
          end_id: '114514',
          last_id: '19191',
          results: [],
          total_count: 0
        },
        chatList: [],
      })
    } else {
      this.setData({
        repeatList: []
      })
    }
    this.getChat(id , size)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.loadEnd) {
      this.getChat(this.data.workData.art_id)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})